<template>
	<view class="code">
		<view class="">
			<u-navbar :autoBack='true'></u-navbar>
		</view>
		<view class="code-main">
			<view class="code-msg">输入6位验证码</view>
			<view class="code-time">
				<view class="">
					验证码发送到 {{setPhone}}
				</view>
				<view class="" v-if="isShow">{{coldeNum}}s后重发</view>
				<view v-if="!isShow" @click="sendCode">
					收不到验证码？
				</view>
			</view>
			<view class="code-num">
				<u-code-input v-model="valueCode" :maxlength="6"></u-code-input>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapMutations
	} from 'vuex'
	import {
		sendRegisterOrLoginCaptcha,
		loginByMobile
	} from '@/api/login.js'
	import {
		Encrypt
	} from '@/utils/aes.js'
	export default {
		data() {
			return {
				phone: '',
				coldeNum: 10,
				valueCode: '',
				isShow: true
			}
		},
		computed: {
			setPhone() {
				let arr = this.phone.split('')
				arr.splice(3, 0, ' ')
				arr.splice(8, 0, ' ')
				return arr.join('')
			}
		},
		onLoad(e) {
			this.phone = e.tel
			this.sendCode()
			// sendRegisterOrLoginCaptcha({
			// 	mobile: this.phone
			// })

		},
		methods: {
			...mapMutations(['setToken']),
			sendCode() {
				this.isShow = true
				let time = setInterval(() => {
					this.coldeNum--
					if (this.coldeNum == 0) {
						this.coldeNum = 10
						this.isShow = false
						clearInterval(time)
					}
				}, 1000)
			}
		},
		watch: {
			valueCode(newv, oldv) {
				if (newv.length == 6) {
					this.$refs.uToast.show({
						message: '正在登录中...',
						type: 'loading',
						debugger: 5000
					})
					loginByMobile({
						mobile: Encrypt(this.phone),
						captcha: newv
					}).then(data => {
							this.setToken(data.accessToken)
							uni.switchTab({
								url: '/pages/tabs/me'
							})
					})
				}
			}
		}
	}
</script>

<style>
	.code {
		width: 100vw;
		height: 100vh;
		background: rgba(255, 255, 255, 0.39);
	}

	.code-main {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		padding-top: 233rpx;
		padding-left: 62rpx;
		padding-right: 62rpx;
	}

	.code-msg {
		font-size: 56rpx;
		font-weight: bold;
		color: #343234;
	}

	.code-time {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 33rpx;
		width: 100%;
		font-size: 28rpx;
		font-weight: 400;
		line-height: 36rpx;
		color: #343234;
		opacity: 0.53;
	}

	.code-num {
		padding-top: 83rpx;
		display: flex;
		justify-content: center;
		width: 100%;
	}
</style>